package com.jzr.open.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.open.db1.model.TInspectResult;
import com.jzr.open.db1.service.InspectResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/inspectResult")
public class InspectResultController {
    @Autowired
    private InspectResultService inspectResultService;

    @PostMapping("/get")
    public Result<List<TInspectResult>> get(@RequestBody TInspectResult record){
        if(U.isBlank(record.getDocNo())){
            return Result.fail("docNo不能为空");
        }
        QueryWrapper<TInspectResult> qw = new QueryWrapper<>();
        qw.orderByAsc("item_code");
        qw.eq(TInspectResult.DocNo_,record.getDocNo());
        List<TInspectResult> list = inspectResultService.list(qw);
        return Result.success("成功",list,list==null ? 0:list.size());
    }

    @PostMapping("/post")
    public Result<Integer> post(@RequestBody List<TInspectResult> list){
        if(list == null || list.isEmpty()){
            return Result.fail("数据不能为空");
        }
        for(TInspectResult record : list){
            if(U.isBlank(record.getDocNo())){
                return Result.fail("docNo不能为空");
            }
            if(U.isBlank(record.getItemCode())){
                return Result.fail("itemCode不能为空");
            }
            if(U.isBlank(record.getItemName())){
                return Result.fail("itemName不能为空");
            }
        }
        int count = inspectResultService.save(list);
        if(count <= 0){
            return Result.fail("保存失败");
        }else {
            return Result.success("成功",count,count);
        }
    }
//    @PostMapping("/delete")
//    public Result<Integer> delete(@RequestBody List<TInspectResult> list){
//        if(list == null || list.isEmpty()){
//            return Result.fail("数据不能为空");
//        }
//        for(TInspectResult record : list){
//            if(U.isBlank(record.getDocNo())){
//                return Result.fail("docNo不能为空");
//            }
//            if(U.isBlank(record.getItemCode())){
//                return Result.fail("itemCode不能为空");
//            }
//        }
//        int count = inspectResultService.delete(list);
//        if(count <= 0){
//            return Result.fail("删除失败");
//        }else {
//            return Result.success("成功",count);
//        }
//    }
}
